<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" inline label-width="68px">
      <el-form-item label="机构名称" prop="institutionId">
        <el-select
          v-model="queryParams.institutionId"
          placeholder="请选择机构名称"
          filterable
          clearable
          @change="getStoreList"
        >
          <el-option
            v-for="item in institutionList"
            :key="item.id"
            :label="item.name"
            :value="item.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="门店名称" prop="storeId">
        <el-select
          v-model="queryParams.storeId"
          placeholder="请选择门店名称"
          filterable
          clearable
          @change="handleQuery"
        >
          <el-option
            v-for="item in storeList"
            :key="item.id"
            :label="item.name"
            :value="item.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="序列号" prop="serialNumber">
        <el-input
          v-model="queryParams.serialNumber"
          placeholder="请输入序列号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">
          搜索
        </el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">
          重置
        </el-button>
      </el-form-item>
    </el-form>
    <el-table v-loading="loading" :data="dataList">
      <el-table-column label="序号" type="index" width="80" align="center" />
      <el-table-column label="柜机名称" align="center" prop="name" />
      <el-table-column label="序列号" align="center" prop="serialNumber" />
      <el-table-column label="机构名称" align="center" prop="institutionName" />
      <el-table-column label="门店名称" align="center" prop="storeName" />
      <el-table-column label="型号" align="center">
        <template slot-scope="scope">
          <span>{{ models[scope.row.model] }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['mc:online:edit']"
          >
            控制面板
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
  </div>
</template>

<script>
import { listOnline } from "@/api/mc/cabinet";
import { listInstitution } from "@/api/mc/institution";
import { listStore } from "@/api/mc/store";

export default {
  name: "online",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 总条数
      total: 0,
      // 表格数据
      dataList: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        institutionId: null,
        storeId: null,
        serialNumber: null,
      },
      models: {
        0: "未知",
        2: "两组药架",
        4: "四组药架",
        6: "六组药架",
      },
      institutionList: [], // 机构列表
      storeList: [], // 门店列表
    };
  },
  created() {
    this.getInstitutionList();
    this.getList();
  },
  methods: {
    /** 查询出药记录列表 */
    getList() {
      this.loading = true;
      listOnline(this.queryParams)
        .then((res) => {
          this.dataList = res.rows;
          this.total = res.total;
        })
        .finally(() => {
          this.loading = false;
        });
    },
    // 获取机构列表
    getInstitutionList() {
      listInstitution({ pageNum: 1, pageSize: 1000 }).then((res) => {
        this.institutionList = res.rows;
      });
    },
    // 获取门店列表
    getStoreList() {
      this.queryParams.storeId = null;
      if (this.queryParams.institutionId) {
        listStore({
          pageNum: 1,
          pageSize: 1000,
          institutionId: this.queryParams.institutionId,
        }).then((res) => {
          this.storeList = res.rows;
        });
      } else {
        this.storeList = [];
      }
      this.handleQuery();
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.storeList = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.$router.push({ path: "/online/control", query: { id: row.id } });
    },
  },
};
</script>
